/*****************************************************************************
Copyright: 2012-2016, Yes Optoelectronics Co.,Ltd.
File name: contrastAdj.h
Description:  functions to adjust LCM contrast value
Author: zhaitao
Version: 1.0.0
Date: 2016-03-23 12:59:57
History:	2016-03-23 13:00:28		Version: 1.0.0		initial version
*****************************************************************************/


#ifndef	__CONTRASTADJ_H__
#define	__CONTRASTADJ_H__



/*  contras add & dec with buttons */
//sfr 	P4    		= 		0xe8;
sbit	key_pause		=		  P2^5;
sbit 	key_up			= 		P2^6;
sbit 	key_down 		= 		P2^7;

/* time intervals */
unsigned long delay3 = 0x0fff;		//image interval small
unsigned long delay2 = 0x08ff;		//contras adjust interval
unsigned long delay1 = 0x4fff;		//image interval large

/* contrast values */
//V0 = RGR X [ 1- (255 - CT) /648 ] X 2.1, or V0 = RGR X [ (393 +CT ) /648 ] X 2.1
const unsigned char 	ConstrstReg		= 	0x81;
//const unsigned char 	ConstrstReg		= 	0xa2;

const unsigned char 	ContrastMax 	= 	255;
const unsigned char 	ContrastMin 	= 	0;
unsigned char 			  contrast 		= 	224;
//unsigned char 			  contrast 		= 	180;







/* 
 *FUNCTION DECLEAR 
 */
void pauseBig(void);
void pauseSmall(void);

void __contrst_adj(unsigned long num);
void __contrast_add(void);
void __contrast_dec(void);
void __pause(unsigned long num);










/* 
 * FUNCTION IMPLEMENTS 
 */

void __contrast_add(void){	
	if(contrast < ContrastMax){
		contrast++;
		write_com(0x23);
		write_com(ConstrstReg);
		write_com(contrast);	
	}	
}

void __contrast_dec(void){
	if(contrast > ContrastMin){
		contrast--;
		write_com(0x23);
		write_com(ConstrstReg);
		write_com(contrast);	
	}	
}

void __contrst_adj(unsigned long num) {
	while(num!=0) {
		if(key_down == 0 || key_up == 0) {
			if(key_down==0) { 
				delay_ms(10); 
				while(key_down==0)	delay_ms(10); 
				num=1;
				__contrast_dec();
			}
			if(key_up==0) { 
				delay_ms(10); 
				while(key_up==0)	delay_ms(10); 
				num=1;
				__contrast_add();
			}
		}
		num--; 
	}
}

void __pause(unsigned long num) {
	unsigned char key=0;		//
	while(num!=0) {				//
		key=key_pause;			//
		if(key==0) {			//
			while(key==0) {		//
				delay_ms(5);	
				key=key_pause;	//
			}

			while(key==1) {		//
				__contrst_adj(delay2);
				key=key_pause;	//
			}
			
			while(key==0) {		//
				delay_ms(5);
				key=key_pause;	//
			}
			num=1;				//
		}
		num--;
	}
}

void pauseBig(void) {
	__pause(delay1);
}
void pauseSmall(void) {
	__pause(delay3);
}
 
 
#endif	//__CONTRASTADJ_H__ 
